import {message} from 'ant-design-vue'
import {getCompanyInfoById, getJobInfoById} from "@/api/company.js";
import {getApplication, getStuInfoByStuId} from "@/api/stu.js";
import {getResumeFile} from "@/api/file.js";

// 更新消息方法
export const updateInterview = async () => {
    try {
        const res = await getApplication();
        if (res.data.code === 200) {
            const applications = res.data.data;

            return await Promise.all(
                applications.map(async (item) => {
                    const companyInfo = await getCompanyInfo(item.companyId);  // 调用工具类方法
                    const jobInfo = await getJobInfo(item.jobId);              // 调用工具类方法

                    // 将公司和职位信息加到session中
                    return {
                        ...item,
                        companyInfo,
                        jobInfo,
                    };
                })
            );
        } else {
            throw new Error(res.data.message);
        }
    } catch (error) {
        message.error('获取申请列表失败');
    }
};

export const getCompanyInfo = async (companyId) => {
    try {
        const res = await getCompanyInfoById({ companyId });
        if (res.data.code === 200) {
            return res.data.data; // 返回公司信息
        } else {
            throw new Error(res.data.message)
        }
    } catch (error) {
        message.error('获取公司信息失败');
    }
};

export const getJobInfo = async (jobId)=>{
    try{
        const res = await getJobInfoById({
            jobId: jobId
        })
        if(res.data.code === 200){
            return res.data.data
        }else {
            throw new Error(res.data.message)
        }
    }catch{
        message.error('获取岗位信息失败')
    }
}
